<template>
  <div class="page" @scroll="paperScroll" ref="page">
    <div class="head">
      <h1>{{ $t('城堡') }}</h1>
    </div>
    <help class="help">
      <div>
        <p>城堡有8个方向每个方向8个城堡，总计64个城堡。</p>
      </div>
    </help>
    <sticky>
      <div class="filter">
        <ul class="status">
          <li v-for="(item, index) in category" :class="{'active': index == params.mine}" @click="changeCategory(index)" :key="index">{{$t(item)}}</li>
        </ul>
        <div class="right">
          <ul class="order">
            <li :class="{'active': item.value == params.field,'up': params.order}" @click="changeOrder(item.value)" v-for="(item, index) in order" :key="index">{{$t(item.name)}}</li>
          </ul>
          <div class="btns">
            <div class="option" @click="showFilter = true"></div>
          </div>
        </div>
      </div>
    </sticky>
    <!-- 我的猴子 -->
    <div class="monkeys">
      <div class="panel" v-for="(item,index) in homeList" :key="index">
        <div class="list-item" @click="toTower(item.id)">
          <div class="img">
            <!-- castleImgUrl -->
            <img :src="item.castleImgUrl" :style="{background: '#eafee1'}">
          </div>
          <ul class="minfo">
            <li>{{ item.castleName }}（{{item.castleNickname}}）</li>
            <li>城主：{{item.castleOwnerName}}</li>
            <li>状态：
              <count-down v-if="resetTimer" :startTime="item.systemTime" :endTime="item.canAttackTime" endText="可攻击" surfixText="后可攻击"></count-down>
            </li>
            <li>昨日收益：{{ Math.floor(item.profit || 0) }} 毛<br>掠={{Math.floor(item.sProfit)}}毛/破={{Math.floor(item.wProfit)}}毛/库={{Math.floor(item.hProfit)}}毛</li>
            <li></li>
          </ul>
        </div>
      </div>
      <empty v-if="homeList.length == 0"></empty>
    </div>
    <!-- 复杂筛选 -->
    <div v-transfer-dom>
      <popup v-model="showFilter" position="right" width="300px" class="filters">
        <div class="wrap">
          <div class="options">
            <h2>方位</h2>
            <ul>
              <li v-for="(item,index) in directions" :key="index" :class="{'active': index+1 === params.xid}" @click="params.xid = index + 1">{{item}}</li>
            </ul>
          </div>
          <div class="options">
            <h2>序号</h2>
            <ul>
              <li v-for="(item,index) in number" :key="index" :class="{'active': index+1 === params.yid}" @click="params.yid = index + 1">{{item}}</li>
            </ul>
          </div>
          <div class="options">
            <h2>等级</h2>
            <ul>
              <li v-for="(item,index) in level" :key="index" :class="{'active': index+1 === params.level}" @click="params.level = index + 1">{{item}}</li>
            </ul>
          </div>
          <div class="options">
            <h2>状态</h2>
            <ul>
              <li v-for="(item,index) in status" :key="index" :class="{'active': index === params.status}" @click="params.status = index">{{item}}</li>
            </ul>
          </div>
        </div>
        <div class="button">
          <x-button type="warn" class="btn" @click.native="resetFilter">{{$t('重置')}}</x-button>
          <x-button type="primary" class="btn" @click.native="enterFilter">{{$t('确定')}}</x-button>
        </div>
      </popup>
    </div>
  </div>
</template>

<i18n>
首页:
  en: Home
全部:
  en: All
集市:
  en: Market
鹊桥:
  en: Sire
历史:
  en: Records
代数:
  en: Gen
代:
  en: Gen
次:
  en: times
体重:
  en: Weight
收益:
  en: Income
投食进度:
  en: Feeding progress
昨日掘金:
  en: Yesterday income
不可生育:
  en: Sterilization
生育记录:
  en: birth records
交易记录:
  en: trade records
下次生育:
  en: Next birth
已复制:
  en: Copied
</i18n>

<script>
  import {
    Group,
    XInput,
    Sticky,
    TransferDom,
    Spinner,
    Popup,
    XButton
  } from "vux";
  import lottery from "./common/lottery.vue";
  import empty from "./common/empty.vue";
  import CountDown from "./common/vue2-countdown.vue";
  import InfiniteLoading from "vue-infinite-loading";
  import Help from './common/help.vue'
  export default {
    directives: {
      TransferDom
    },
    components: {
      Group,
      XInput,
      lottery,
      Sticky,
      empty,
      CountDown,
      InfiniteLoading,
      Spinner,
      Popup,
      XButton,
      Help
    },
    data() {
      return {
        order: [{
            name: "名称",
            value: 0
          },
          {
            name: "收益",
            value: 1
          }
        ],
        category: ['全部', '我的'],
        directions: ['甲', '乙', '丙', '丁', '戊', '己', '庚', '辛'],
        number: ['一', '二', '三', '四', '五', '六', '七', '八'],
        level: ['Lv1', 'Lv2', 'Lv3', 'Lv4', 'Lv5'],
        status: ['可攻击', '保护期', 'NPC占领'],
        homeList: [],
        params: {
          order: true,
          field: 0,
          status: '',
          xid: '',
          yid: '',
          level: '',
          mine: 0
        },
        showFilter: false,
        scrollTop_city: 0,
        resetTimer: true
      };
    },
    mounted() {
      this.$nextTick(function() {
        this.initData()
      });
    },
    activated() {
      this.$refs.page.scrollTop = this.$store.state.scrollTop_city;
    },
    methods: {
      initData() {
        this.$http
          .post(`/gameCastle/query`, this.params)
          .then(res => {
            if (res.code == 200) {
              this.homeList = res.result
            } else {
              this.$vux.toast.text(res.msg);
            }
          })
          .catch(function(error) {});
      },
      resetFilter() {
        this.params.status = "";
        this.params.xid = "";
        this.params.yid = "";
        this.params.level = "";
      },
      enterFilter() {
        this.resetTimer = false
        this.initData();
        this.showFilter = false;
        setTimeout(() => {
          this.resetTimer = true
        }, 200);
      },
      changeCategory(index) {
        this.params.mine = index;
        this.initData()
      },
      paperScroll(e) {
        this.scrollTop_city = e.srcElement.scrollTop;
      },
      toTower(id) {
        this.$store.commit("scrollTop", {
          name: "scrollTop_city",
          value: this.scrollTop_city
        });
        this.$router.push(`/tower/${id}`);
      },
      changeOrder(order) {
        //点击同一个
        if (order == this.params.field) {
          this.params.order = !this.params.order
        } else {
          this.params.field = order;
          this.params.order = true;
        }
        this.initData();
      }
    }
  };
</script>

<style lang="scss" scoped>
  .head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .btns {
      display: flex;
      align-items: center;
      a {
        display: inline-block;
        padding: 2px 5px;
        border: 1px solid $tmdark;
        border-radius: 5px;
        font-size: 12px;
        color: $tmdark;
      }
    }
    .option {
      width: 28px;
      height: 28px;
      margin-left: 5px;
      background: url() no-repeat 50%;
      background-size: 16px 16px;
    }
  }
  .filter {
    display: flex;
    justify-content: space-between;
    white-space: nowrap; // flex-wrap: wrap;
    font-size: 12px;
    background: #fff;
    height: 40px;
    line-height: 40px;
    padding: 0 12px;
    margin: 0 8px 12px;
    border-radius: 4px;
    box-shadow: 3px 3px 10px 0px rgba(158, 158, 158, 0.4);
    overflow-x: scroll;
    overflow-y: hidden;
    &::-webkit-scrollbar {
      display: none;
    }
    ul {
      display: flex;
      align-items: center;
      li {
        cursor: pointer;
      }
    }
    .right {
      display: flex;
    }
    .status {
      color: $grey;
      li {
        transition: all .3s;
      }
      li:not(:last-child) {
        margin-right: 12px;
      }
      .active {
        color: $green;
        font-size: 14px;
        border-bottom: 2px solid $green;
      }
    }
    .order {
      color: $link;
      li+li {
        margin-left: 12px;
      }
      .active:after {
        content: "";
        display: inline-block;
        vertical-align: middle;
        margin-top: -2px;
        width: 12px;
        height: 12px;
        background: url() no-repeat 50%;
        background-size: cover;
      }
      .active.up:after {
        transform: rotate(-180deg);
      }
    }
    .btns {
      display: flex;
      align-items: center;
    }
    .option {
      width: 28px;
      height: 28px;
      margin-left: 5px;
      background: url() no-repeat 50%;
      background-size: 16px 16px;
    }
  }
  .filters {
    overflow: hidden;
    background: #fbf9fe;
    &::-webkit-scrollbar {
      display: none;
    }
    .wrap {
      padding-bottom: 60px;
      height: 100%;
      overflow-y: scroll;
      padding: 12px;
    }
    .options {
      h2 {
        font-size: 14px;
        color: #666;
        font-weight: 500;
        margin-top: .77em;
        margin-bottom: .3em;
      }
      ul {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        li {
          background: #f1f2f6;
          text-align: center;
          height: 26px;
          line-height: 26px;
          width: 30%;
          margin-bottom: 5px;
          border-radius: 4px;
          color: #666;
          border: 1px solid transparent;
        }
        .active {
          color: #df443b;
          border: 1px solid #df443b;
          background: #fff;
        }
        &:after {
          content: "";
          width: 30%;
        }
      }
    }
    .button {
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      display: flex;
      .btn {
        width: 50%;
        height: 40px;
        margin: 0;
        border: 0;
        border-radius: 0;
        &:after {
          display: none;
        }
      }
    }
  }
  .list-item {
    position: relative;
    display: flex;
    height: 100px;
    overflow: hidden;
    flex-shrink: 1;
    .img {
      width: 100px;
      height: 100px;
      font-weight: 300;
      margin-right: 10px;
      position: relative;
      flex-shrink: 0;
      font-size: 10px;
      border-radius: 4px;
      overflow: hidden;
      @media only screen and (min-width: 375px) {
        font-size: 10px;
      }
      @media only screen and (min-width: 414px) {
        font-size: 12px;
      }
      @media only screen and (min-width: 768px) {
        font-size: 14px;
      }
      .id {
        position: absolute;
        color: $grey;
        background-color: rgba(255, 255, 255, 0.8);
        left: 0;
        top: 6px;
        padding: 0 5px 0 0;
        border-radius: 0 100px 100px 0;
      }
      img {
        width: 100%;
      }
      .info {
        position: absolute;
        left: 2px;
        bottom: 2px;
        line-height: 14px;
        color: #000;
        width: 100%;
        white-space: nowrap;
        padding: 2px;
        p {
          width: 100%;
          overflow: hidden;
          text-overflow: ellipsis;
        }
      }
    }
    .minfo {
      display: flex;
      flex: 1;
      overflow: hidden;
      justify-content: space-between;
      flex-direction: column;
      font-size: 14px;
      color: $grey;
      white-space: nowrap;
      li:first-child {
        color: #000;
      }
      li {
        overflow: hidden;
        text-overflow: ellipsis;
      }
      span {
        vertical-align: middle;
      }
    }
    .tag {
      position: absolute;
      top: -3px;
      right: 0;
      color: $green;
    }
  }
</style>
